const mongoose = require('mongoose');

const articleSchema = new mongoose.Schema({
    title: {
        type: String,
        required: [true, '标题是必需的'],
        trim: true,
        minlength: [2, '标题至少需要2个字符'],
        maxlength: [100, '标题不能超过100个字符']
    },
    content: {
        type: String,
        required: [true, '内容是必需的'],
        minlength: [10, '内容至少需要10个字符']
    },
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    tags: [{
        type: String,
        trim: true
    }],
    status: {
        type: String,
        enum: ['draft', 'published'],
        default: 'published'
    },
    likes: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }],
    comments: [{
        user: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        },
        content: String,
        createdAt: {
            type: Date,
            default: Date.now
        }
    }],
    createdAt: {
        type: Date,
        default: Date.now
    },
    updatedAt: {
        type: Date,
        default: Date.now
    }
});

// 更新时自动更新updatedAt字段
articleSchema.pre('save', function(next) {
    this.updatedAt = Date.now();
    next();
});

const Article = mongoose.model('Article', articleSchema);

module.exports = Article; 